package com.zhilianbao.leyaogo.http.convert;

import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.zhilianbao.leyaogo.model.response.home.BaseHomeData;
import com.zhilianbao.leyaogo.model.response.home.HomeAppSearch;
import com.zhilianbao.leyaogo.ui.adapter.home.applista.HomeAppListAOne;
import com.zhilianbao.leyaogo.ui.adapter.home.applista.HomeAppListATwoThree;
import com.zhilianbao.leyaogo.ui.adapter.home.applista.HomeAppListAZero;
import com.zhilianbao.leyaogo.ui.adapter.home.grids.HomeAppGrids;
import com.zhilianbao.leyaogo.ui.adapter.home.magic.HomeAppMagic;
import com.zhilianbao.leyaogo.ui.adapter.home.notice.HomeAppNotice;
import com.zhilianbao.leyaogo.ui.adapter.home.slider.HomeAppSlider;

import java.lang.reflect.Type;

/**
 * ClassName: HomeSortableDeserializer <br>
 * Description: 首页排序数据deserializer<br>
 * Author: Cyarie <br>
 * Created: 2016/12/27 17:26 <br>
 * Update Time：<br>
 * Update Description：<br>
 */
public class HomeSortableDeserializer implements JsonDeserializer<BaseHomeData> {

    /**
     * 搜索栏
     */
    public static final String APP_SEARCH = "appSearch";


    /**
     * Banner
     */
    public static final String APP_SLIDER = "appSlider";

    /**
     * 九宫格栏目
     */
    public static final String APP_GRIDS = "appGrids";


    /**
     * 跑马灯广告
     */
    public static final String APP_NOTICE = "appNotice";

    /**
     * 商品列表 A
     */
    public static final String APP_LIST_A = "appListA";

    /**
     * 魔方区域
     */
    public static final String APP_MAGIC = "appMagic";


    @Override
    public BaseHomeData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        Gson gson = GsonConvert.createDeserializer();
        JsonObject jsonObject = (JsonObject) json;
        final String contentType = stringOrEmpty(jsonObject.get("name"));
        BaseHomeData content = null;
        if (contentType.equals(APP_SEARCH)) {
            content = gson.fromJson(json, HomeAppSearch.class);
        } else if (contentType.equals(APP_SLIDER)) {
            content = gson.fromJson(json, HomeAppSlider.class);
        } else if (contentType.equals(APP_GRIDS)) {
            content = gson.fromJson(json, HomeAppGrids.class);
        } else if (contentType.equals(APP_NOTICE)) {
            content = gson.fromJson(json, HomeAppNotice.class);
        } else if (contentType.equals(APP_MAGIC)) {
            content = gson.fromJson(json, HomeAppMagic.class);
        } else if (contentType.equals(APP_LIST_A)) {
            switch (intOrEmpty(jsonObject.get("data").getAsJsonObject().get("type"))) {
                //直接返回列表格式
                case 0:
                    content = gson.fromJson(json, HomeAppListAZero.class);
                    break;

                case 1:
                    content = gson.fromJson(json, HomeAppListAOne.class);
                    break;

                case 2:
                case 3:
                    content = gson.fromJson(json, HomeAppListATwoThree.class);
                    break;
            }

        }
        return content;
    }


    private String stringOrEmpty(JsonElement jsonElement) {
        return jsonElement.isJsonNull() ? "" : jsonElement.getAsString();
    }

    private int intOrEmpty(JsonElement jsonElement) {
        return jsonElement.isJsonNull() ? 0 : jsonElement.getAsInt();
    }
}
